package com.sx.service.serviceImpl;

import com.sx.dao.CategoryDao;
import com.sx.dao.daoImpl.CategoryDaoImpl;
import com.sx.pojo.Category;
import com.sx.pojo.OptionRecords;
import com.sx.service.CategoryService;

public class CategoryServiceImpl implements CategoryService {
    private final CategoryDao categoryDao = new CategoryDaoImpl();
    private final OptionRecordServiceImpl optionRecordService = new OptionRecordServiceImpl();

    @Override
    public boolean checkIdExists(String id) {
        return categoryDao.checkIdExists(id);
    }

    /*@Override
    public boolean saveCategory(Category category) {
        return categoryDao.addCategory(category);
    }*/
    public boolean saveCategory(Category category, String PersonId, String PersonName, int flag, int type, String id) {
        OptionRecords optionRecords = new OptionRecords();
        optionRecords.setPersonId(PersonId);
        optionRecords.setPersonName(PersonName);
        optionRecords.setFlag(flag);
        optionRecords.setType(type);
        optionRecords.setBeOption(null);
        optionRecords.setAfOption(category.toString());
        if (categoryDao.addCategory(category)) {
            optionRecordService.addOptionRecord(optionRecords.getPersonId(), optionRecords.getPersonName(), optionRecords.getBeOption(), optionRecords.getAfOption(), optionRecords.getFlag(), optionRecords.getType());
            return true;
        } else {
            return false;
        }
    }

    /*@Override
    public boolean deleteCategoryById(String id) {
        return categoryDao.deleteCategoryById(id);
    }*/
    public boolean deleteCategoryById(String PersonId, String PersonName, int flag, int type, String id) {
        OptionRecords optionRecords = new OptionRecords();
        optionRecords.setPersonId(PersonId);
        optionRecords.setPersonName(PersonName);
        optionRecords.setFlag(flag);
        optionRecords.setType(type);
        optionRecords.setBeOption(categoryDao.getCategoryById(id).toString());
        optionRecords.setAfOption(null);
        if (categoryDao.deleteCategoryById(id)) {
            optionRecordService.addOptionRecord(optionRecords.getPersonId(), optionRecords.getPersonName(), optionRecords.getBeOption(), optionRecords.getAfOption(), optionRecords.getFlag(), optionRecords.getType());
            return true;
        } else {
            return false;
        }
    }


    @Override
    public Category getCategoryById(String id) {
        return categoryDao.getCategoryById(id);
    }

    /*@Override
    public boolean updateCategory(Category category) {
        return categoryDao.updateCategory(category);
    }*/
    public boolean updateCategory(Category category, String PersonId, String PersonName, int flag, int type, String id) {
        OptionRecords optionRecords = new OptionRecords();
        optionRecords.setPersonId(PersonId);
        optionRecords.setPersonName(PersonName);
        optionRecords.setFlag(flag);
        optionRecords.setType(type);
        optionRecords.setBeOption(categoryDao.getCategoryById(id).toString());
        optionRecords.setAfOption(category.toString());
        if (categoryDao.updateCategory(category)) {
            optionRecordService.addOptionRecord(optionRecords.getPersonId(), optionRecords.getPersonName(), optionRecords.getBeOption(), optionRecords.getAfOption(), optionRecords.getFlag(), optionRecords.getType());
            return true;
        } else {
            return false;
        }
    }
}
